/* List the USB keyboard devices, in /dev/input 
 * These codes are used in MIDPath-patch. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <termios.h>
#include <sys/vt.h>
#include <sys/time.h>
#include <sys/types.h>

#include <linux/fb.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
#include <linux/input.h>

#define test_bit(bit, array)    (array[bit/8] & (1<<(bit%8)))

int main()
{
	char *devices[]={
		"/dev/input/event0", "/dev/input/event1", "/dev/input/event2", "/dev/input/event3", 
		"/dev/input/event4", "/dev/input/event5", "/dev/input/event6", "/dev/input/event7", 
		"/dev/input/event8", "/dev/input/event9",
	};
	int i;
	for(i=0;i<sizeof(devices)/sizeof(char *);i++){
		char *device_name = devices[i];
		int fd = open(device_name, O_RDWR);
		if (fd < 0) continue;
		// Check if the opened device is a keyboard
		uint8_t evtype_bitmask[EV_MAX/8 + 1];
		if (ioctl(fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) >= 0) {
			if (test_bit(EV_KEY, evtype_bitmask) && test_bit(EV_LED, evtype_bitmask) && test_bit(EV_REP, evtype_bitmask))
				printf("%s\n", device_name);
		}
		close(fd);
	}

	return 0;
}
